// ZobristTable.cpp

#include "includes.h"

ZobristTable::ZobristTable()
{
	FILE * fRandom = fopen("/dev/random", "r");

	// We will need 3120 bytes of random data to populate the table
	if (!fread(zTable, 1, 3120, fRandom) == 3120)
	{
		printf("FATAL ERROR: fread() did not return 3120 bytes from " 
				   "/dev/random\n");
		exit(EXIT_FAILURE);
	}

	if (!fread(&blackOnMove, 8, 1, fRandom))
	{
		printf("FATAL ERROR: fread() did not return 8 bytes from " 
				   "/dev/random\n");
		exit(EXIT_FAILURE);
	}
	
	if (!fread(&whiteOnMove, 8, 1, fRandom))
	{
		printf("FATAL ERROR: fread() did not return 8 bytes from " 
				   "/dev/random\n");
		exit(EXIT_FAILURE);
	}

	return;
}

// This method calculates a hash value for the board pointed to by pBoard.
unsigned long ZobristTable::hash(board * pBoard)
{
	unsigned long hash = 0;
	unsigned int position = A6; //highest order position on bit board
	int i = 0; 
	int j = 0;
	for (i = 0; i < 30; i++)
	{
		j = findPiece(pBoard, position);
		hash = hash + zTable[i][j];
		position = position >> 1;
	}

	return hash;
}

